package com.techsen.tsweb.sys.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.techsen.tsweb.core.domain.JsonResult;
import com.techsen.tsweb.core.util.ValidUtil;
import com.techsen.tsweb.core.util.sql.Pager;
import com.techsen.tsweb.sys.domain.Controller;
import com.techsen.tsweb.sys.service.ControllerService;

@RequestMapping("/sys/controller")
@org.springframework.stereotype.Controller
public class ControllerController {

    @Resource
    private ControllerService controllerService;
    
    /**
     * 进入控制器管理界面
     */
    @RequestMapping
    public String index(String menuId, Model model) {
        model.addAttribute("menuId", menuId);
        return "sys/controller";
    }
    
    /**
     * 查看控制器
     */
    @RequestMapping("/{id}")
    public String view(@PathVariable("id") String id, Model model) {
        if (ValidUtil.isValid(id)) {
            Controller controller = this.controllerService.get(id);
            model.addAttribute("controller", controller);
        }
        return "sys/controller-view";
    }
    
    /**
     * 获取所有的控制器
     */
    @RequestMapping("/list")
    public @ResponseBody List<Controller> list() {
        return this.controllerService.findAll();
    }
    
    /**
     * 分页查询控制器
     */
    @RequestMapping("/pager")
    public @ResponseBody Pager<Controller> pager(@RequestBody Pager<Controller> pager) {
        return this.controllerService.findByPager(pager);
    }
    
    /**
     * 进入添加控制器界面
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add() {
        return "sys/controller-add";
    }
    
    /**
     * 添加控制器
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public @ResponseBody JsonResult add(Controller controller) {
        this.controllerService.add(controller);
        return new JsonResult(controller);
    }
    
    /**
     * 删除控制器
     */
    @RequestMapping("/delete")
    public @ResponseBody JsonResult delete(String id) {
        this.controllerService.delete(id);
        return new JsonResult(id);
    }
    
    /**
     * 进入修改控制器界面
     */
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(String controllerId, Model model) {
        Controller controller = this.controllerService.get(controllerId);
        model.addAttribute("controller", controller);
        return "sys/controller-update";
    }
    
    /**
     * 修改控制器
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public @ResponseBody JsonResult update(Controller controller) {
        this.controllerService.update(controller);
        return new JsonResult(controller);
    }
    
}
